package personal.bean;

import java.io.Serializable;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.TreeNode;

import resources.dao.DepartmentDAO;
import resources.pojo.pe_deptmt;
import resources.util.Tool;
import resources.util.HVUtil;

@ManagedBean(name = "departmentManagement")
@SessionScoped
public class DepartmentManagement implements Serializable {

	private static final long serialVersionUID = 1L;
	private TreeNode root;
	private ArrayList<pe_deptmt> selectDepartment1; // create
	private ArrayList<pe_deptmt> selectDepartment2; // update
	private pe_deptmt selectNode;
	private pe_deptmt insertNode;
	private int partid;

	public void onload() {
		if (!Tool.isPostBack()) {
			init();
		}
	}

	public void init() {
		selectNode = new pe_deptmt();
		insertNode = new pe_deptmt();
		selectDepartment1 = new ArrayList<pe_deptmt>();
		selectDepartment2 = new ArrayList<pe_deptmt>();
		HVUtil.open();
		root = (TreeNode)DepartmentDAO.getDepartmentTree(); // position 0 is tree;
		root.setExpanded(true);
		selectDepartment1 = DepartmentDAO.getSelectDepartmentManage(); // position 1 is list
		HVUtil.close();
	}

	public void select(NodeSelectEvent event) {
		try {
			selectDepartment2.clear();
			pe_deptmt object = (pe_deptmt) event.getTreeNode().getData();
			selectNode = object;
			if (object.getId() == 1) {
				selectDepartment2.clear();
				return;
			}
			selectDepartment2.addAll(selectDepartment1);
			if (selectDepartment2.size() > 0) {
				partid = selectNode.getPartid();
				for (int i = 0; i < selectDepartment2.size(); i++) {
					if (selectDepartment2.get(i).getId() == object.getId()) {
						selectDepartment2.remove(i);
					}
				}
			}
		} catch (Exception e) {
			Tool.writeError(e.getMessage() + " [ select(NodeSelectEvent event) DepartmentManagement.java]");
		}finally{
			Tool.writeInfo("Select department tree");
		}
	}

	public void update() {
		try {
			if (selectNode.getKywd() == null && selectNode.getKywd().equals("")){
				Tool.message("Tên phòng ban không được để trống");
				return;
			}
			if(selectNode.getId() == 1){
				selectNode.setPartid(0);
			}else{
				selectNode.setPartid(partid);
			}
			HVUtil.open();
			boolean flag = DepartmentDAO.updateDepartment(selectNode);
			HVUtil.close();
			if (flag) {
				Tool.message(Tool.getKey("update-successfully"));
				init();
			} else {
				Tool.message("Error");
			}
		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}

	public void insert() {
		try {
			if (insertNode.getKywd() == null || insertNode.getKywd().equals("")){
				Tool.message(Tool.getKey("department-error-3"));
				return;
			}
			HVUtil.open();
			boolean flag = DepartmentDAO.insertDepartment(insertNode);
			HVUtil.close();
			if (flag) {
				Tool.message(Tool.getKey("insert-successfully"));
				init();
			} else {
				Tool.message("Không thể thêm phòng ban thành công");
			}
		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}

	public void delete() {
		try {
			HVUtil.open();
			int flag = DepartmentDAO.deleteDepartment(selectNode.getId());
			HVUtil.close();
			switch (flag) {
			case 0:
				Tool.message(Tool.getKey("department-error-1"));
				break;
			case 1:
				Tool.message(Tool.getKey("department-error-2"));
				break;
			case 2:
				init();
				Tool.message(Tool.getKey("delete-successfully"));
				break;
			default:
				break;
			}			
		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public TreeNode getRoot() {
		return root;
	}

	public void setSelectNode(pe_deptmt selectNode) {
		this.selectNode = selectNode;
	}

	public pe_deptmt getSelectNode() {
		return selectNode;
	}

	public void setInsertNode(pe_deptmt insertNode) {
		this.insertNode = insertNode;
	}

	public pe_deptmt getInsertNode() {
		return insertNode;
	}

	public void setSelectDepartment1(ArrayList<pe_deptmt> selectDepartment1) {
		this.selectDepartment1 = selectDepartment1;
	}

	public ArrayList<pe_deptmt> getSelectDepartment1() {
		return selectDepartment1;
	}

	public void setPartid(int partid) {
		this.partid = partid;
	}

	public int getPartid() {
		return partid;
	}

	public void setSelectDepartment2(ArrayList<pe_deptmt> selectDepartment2) {
		this.selectDepartment2 = selectDepartment2;
	}

	public ArrayList<pe_deptmt> getSelectDepartment2() {
		return selectDepartment2;
	}
}
